<?php
class ExperienciasController extends AppController {

	var $name = 'Experiencias';
	var $helpers=array('Access');

	function index(){
		$this->Experiencia->recursive = 0;
		
		$id = $this->Access->getMy('id');
		$experiencias=$this->paginate('Experiencia',array('Experiencia.user_id'=>$id));
		$usuario=$this->Experiencia->User->find('first',array('conditions'=>array('User.id')));
		$this->set(compact('experiencias','usuario'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid experiencia', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('experiencia', $this->Experiencia->read(null, $id));
	}

	function add() 
	{	
	  if (!empty($this->data)) 
	  {
		$this->Experiencia->create();
		 if ($this->Experiencia->save($this->data)) 
		 {
		    $this->Session->setFlash(__('La experiencia ha sido guardada', true));
			$this->redirect(array('action' => 'index'));
		 }else{
			   $this->Session->setFlash(__('La experiencia no se ha guardado, por favor intente de nuevo', true));
			  }
	   }	   
	   $users = $this->Experiencia->User->find('list',array('fields'=>array('User.id','User.nombre_completo')));
	   $this->set(compact('users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid experiencia', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Experiencia->save($this->data)) {
				$this->Session->setFlash(__('La experiencia ha sido actualizada', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The experiencia could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Experiencia->read(null, $id);
		}
		$users = $this->Experiencia->User->find('list',array('fields'=>array('User.id','User.nombre_completo')));
		$this->set(compact('users'));
	}

	function delete($id = null)
	{
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for experiencia', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Experiencia->delete($id)) {
			$this->Session->setFlash(__('Experiencia deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Experiencia was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
